package com.boxman;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.Timer;

// 小人
class Man extends Box implements ActionListener {
    private Image mCurrentImage1 = mImage;  // 当前图片1
    private Image mCurrentImage2 = mImage2; // 当前图片2
    private final Component mGameBoard;     // 游戏区域
    private Image mImageLeft1;              // 左图片1
    private Image mImageLeft2;              // 左图片2
    private Image mImageRight1;             // 右图片1
    private Image mImageRight2;             // 右图片2
    private Image mImageUp1;                // 上图片1
    private Image mImageUp2;                // 上图片2
    private final Timer mTimer;             // 定时器

    /**
     * 构造方法。
     * @param back      背后对象
     * @param drawer    绘图者
     * @param gameBoard 游戏区域
     * @param type      类型
     * @param x, y      左上角坐标
     */
    public Man(BaseGameObject back, Graphics drawer, Component gameBoard, Type type, int x, int y) {
        super(back, drawer, type, x, y);

        // 初始化成员
        mGameBoard = gameBoard;
        
        // 加载所有图片
        String imageFilePath;
        File file;
        try {
            imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 2);
            file = new File(imageFilePath);
            mImageLeft1 = ImageIO.read(file);
            
            imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 3);
            file = new File(imageFilePath);
            mImageLeft2 = ImageIO.read(file);
            
            imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 4);
            file = new File(imageFilePath);
            mImageRight1 = ImageIO.read(file);
            
            imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 5);
            file = new File(imageFilePath);
            mImageRight2 = ImageIO.read(file);
            
            imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 6);
            file = new File(imageFilePath);
            mImageUp1 = ImageIO.read(file);
            
            imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 7);
            file = new File(imageFilePath);
            mImageUp2 = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 开始跳舞
        mTimer = new Timer(Const.TIMER_INIT, this);
        mTimer.start();
    }

    /**
     * 定时器事件的响应方法。
     * @param e 事件相关信息
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (mCurrentImage == mCurrentImage1) {
            mCurrentImage = mCurrentImage2;
        } else {
            mCurrentImage = mCurrentImage1;
        }
        mDrawer.drawImage(mCurrentImage, mX * Const.GRID_SIZE, mY * Const.GRID_SIZE, null);
        mGameBoard.repaint(mX * Const.GRID_SIZE, mY * Const.GRID_SIZE, Const.GRID_SIZE, Const.GRID_SIZE);
    }
    
    /**
     * 移动。
     * @param direction     方向
     * @param moveResult    移动结果
     * @return 被推动的箱子
     */
    @Override
    public BaseGameObject move(int direction, MoveResult moveResult) {
        int destX = mX, destY = mY; // 新位置的坐标

        // 根据方向，计算新位置的坐标和图片
        switch (direction) {
            case KeyEvent.VK_DOWN:
                destY++;
                mCurrentImage1 = mImage;
                mCurrentImage2 = mImage2;
                break;
            case KeyEvent.VK_LEFT:
                destX--;
                mCurrentImage1 = mImageLeft1;
                mCurrentImage2 = mImageLeft2;
                break;
            case KeyEvent.VK_RIGHT:
                destX++;
                mCurrentImage1 = mImageRight1;
                mCurrentImage2 = mImageRight2;
                break;
            case KeyEvent.VK_UP:
                destY--;
                mCurrentImage1 = mImageUp1;
                mCurrentImage2 = mImageUp2;
                break;
        }

        // 重新设置图片
        mCurrentImage = mCurrentImage1;

        // 先移动旁边的物体
        final BaseGameObject box = Game.sBaseGameObjects[destY][destX].move(direction, moveResult);
        if (moveResult.result == MoveResult.Result.NO_MOVE) {
            return null;
        }

        // 移动小人
        super.move(direction, moveResult);

        return box;
    }
    
    /**
     * 停止跳舞。
     */
    public void stopDancing() {
        mTimer.stop();
    }
}
